package com.myworkspace.standard.web.controller;

import com.myworkspace.standard.common.base.BaseController;
import com.myworkspace.standard.common.config.UploadConfig;
import com.myworkspace.standard.common.result.Result;
import com.myworkspace.standard.common.result.ResultDict;
import com.myworkspace.standard.web.service.IndexService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author liyang
 */
@RestController
@RequestMapping("api/index")
public class ApiIndexAction extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(BaseController.class.getName());
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate nameJdbcTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UploadConfig uploadConfig;
    @Resource
    private IndexService indexService;


    @RequestMapping("/index")
    public Map<String, Object> toIndex() {
        RedisScript<String> objectRedisScript = new DefaultRedisScript<>("");
        stringRedisTemplate.execute(objectRedisScript, new ArrayList<>());
        Map<String, Object> map = jdbcTemplate.queryForMap("select * from os_shop");
        return map;
    }

    @RequestMapping("/addShop")
    public Result addShop(String phone, String shopName, String desc, String address) {
        // save 上传文件 下载文件 断点续传
        stringRedisTemplate.opsForValue().set("aaaa", "aaaa");

        String uploadPath = uploadConfig.getUploadPath();
        System.out.println(uploadPath);
        Result result = new Result(ResultDict.RESULT_SUCCESS_CODE, "成功");
        return result;
    }

    /**
     * 手机自适应页面
     * 添加信息
     * 列表展示
     */
    @RequestMapping("/uploadFile")
    public Result uploadFile(@RequestParam("file") MultipartFile multipartFile) {
        Result result = new Result(ResultDict.RESULT_SUCCESS_CODE, "成功");
        try {
            indexService.uploadFile(multipartFile);
        } catch (Exception e) {
            logger.error("异常", e);
            result = new Result(ResultDict.RESULT_SYSTEM_EXCEPTION_CODE, "异常");
        }
        return result;
    }

    @GetMapping("/getFileList")
    public Result getFileList() throws Exception {
        Result result = new Result(ResultDict.RESULT_SUCCESS_CODE, "成功");
        List<Map<String, Object>> list = indexService.getFileList();
        result.setResult(list);
        return result;
    }

    @GetMapping("/updateTest")
    public void test() throws Exception {
        indexService.updateTest();
    }


    @GetMapping("/getTest")
    public Result getTest() {
        return new Result(ResultDict.RESULT_SUCCESS_CODE, "成功");
    }
}
